package view;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JPanel;
import javax.swing.Scrollable;
import javax.swing.SwingConstants;

/**
 * The large track panel is the basis for the novel design.
 * Unlike the normal track panel, it will fill the height of its container.
 * @author Hedja
 */
public class LargeTrackPanel extends TrackPanel implements Scrollable {

	public static final int DEFAULT_NOTE_WIDTH = 16;
	public static final int DEFAULT_NOTE_HEIGHT = 22;

	private int noteWidth;

	public LargeTrackPanel(TrackPanel trackPanel) {
		super(trackPanel);
		this.setPaintBeat(true);
		this.setOpaque(false);
		noteWidth = DEFAULT_NOTE_WIDTH;
	}

	/**
	 * Stretches this panel to fit the given height while maintaining each note's
	 * aspect ratio by also adjusting the width accordingly
	 * @param height The height of the new dimension.
	 */
	public void stretch(int height) {
		double newNoteHeight = (double)height/9;
		double multiplier = newNoteHeight/DEFAULT_NOTE_HEIGHT;

		noteWidth = (int)(DEFAULT_NOTE_WIDTH*multiplier);
		for(Component c : this.getComponents()) {
			if(c instanceof JPanel) {
				((JPanel)c).setPreferredSize(new Dimension(noteWidth, height));
			}
		}

		this.revalidate();
	}

	@Override
	public Dimension getPreferredScrollableViewportSize() {
		return this.getPreferredSize();
	}

	@Override
	public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
		return noteWidth*8;
	}

	@Override
	public boolean getScrollableTracksViewportHeight() {
		return false;
	}

	@Override
	public boolean getScrollableTracksViewportWidth() {
		return false;
	}

	@Override
	public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
		int diff = visibleRect.x % noteWidth;
		if(orientation == SwingConstants.HORIZONTAL) {
			if(diff > 0) {
				return direction > 0 ? noteWidth - diff : diff;
			}
			return noteWidth;
		}

		return 0;
	}

}
